package com.cal.utms.model.user_maintenance;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cal.utms.dao.user_maintenance.UserDaoImpl;
import com.cal.utms.dto.user_maintenance.UserGroupDto;
import com.cal.utms.entity.UserGroup;
import com.cal.utms.entity.UserGroupScreen;
import com.cal.utms.entity.UserGroupScreenPK;

public class UserGroupModel {

	UserDaoImpl userDaoImpl = new UserDaoImpl();

	public static List<UserGroupDto> createUserGruopList(List<UserGroup> groups) {
		List<UserGroupDto> groupDtos = new ArrayList<UserGroupDto>();
		for (UserGroup userGroup : groups) {
			UserGroupDto groupDto = new UserGroupDto();
			groupDto.setIsfundManager(userGroup.getFundManager());
			groupDto.setGroupCode(userGroup.getGroupCode());
			groupDto.setGroupName(userGroup.getGroupName());
			groupDto.setId(userGroup.getId());
			groupDtos.add(groupDto);
		}
		return groupDtos;
	}

	public static UserGroup createUserGroupModel(UserGroupDto userGroupDto) {
		UserGroup group = new UserGroup();
		group.setGroupName(userGroupDto.getGroupName());
		group.setGroupCode(userGroupDto.getGroupCode());
		group.setFundManager(userGroupDto.getIsfundManager());
		return group;
	}

	public String saveUserGroup(UserGroupDto userGroupDto, String screenids) {

		if (userDaoImpl.getUserGroupByCode(userGroupDto.getGroupCode()) == null) {
			UserGroup group = createUserGroupModel(userGroupDto);
			List<UserGroupScreen> groupScreens = new ArrayList<UserGroupScreen>();
			if (screenids != null) {
				screenids = "{ \"screenids\" : ".concat(screenids) + " }";
				try {
					JSONObject jsonObject = new JSONObject(screenids);
					JSONArray jArray = jsonObject.getJSONArray("screenids");
					for (int i = 0; i < jArray.length(); i++) {
						JSONObject jObj = jArray.getJSONObject(i);
						if (jObj.getInt("menuid") != 0) {
							if (jObj.getInt("screenId") != 0) {

								UserGroupScreen groupScreen = new UserGroupScreen();
								UserGroupScreenPK groupScreenPK = new UserGroupScreenPK();

								groupScreenPK.setScreenId(jObj.getInt("screenId"));

								groupScreen.setId(groupScreenPK);
								groupScreen.setConfirm(jObj.getBoolean("confirm"));
								groupScreen.setReject(jObj.getBoolean("reject"));
								groupScreen.setSubmit(jObj.getBoolean("create"));
								groupScreen.setView(jObj.getBoolean("view"));

								groupScreens.add(groupScreen);
							}
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			Object object = userDaoImpl.saveUserGroup(group, groupScreens);
			if (object instanceof Integer) {
				userGroupDto.setId((Integer) object);
				userGroupDto.setSuccess(true);
				userGroupDto.setResultMsg("Saved Successfully");
			} else {
				userGroupDto.setSuccess(false);
				userGroupDto.setResultMsg("There is some error returned from the server.");
			}
		} else {
			userGroupDto.setSuccess(false);
			userGroupDto.setResultMsg("Duplicate Code");
		}
		return userGroupDto.getResultMsg();
	}

	public List<UserGroupDto> getAllUserGroup() {

		List<UserGroupDto> groupDtos = UserGroupModel.createUserGruopList(userDaoImpl.getAllUserGruop());
		return groupDtos;
	}

	public String updateGroup(String screenids, int groupId, UserGroupDto userGroupDto) {

		UserGroup userGroup = userDaoImpl.getUserGroupById(groupId);

		if (userGroup != null) {
			userGroup.setFundManager(userGroupDto.getIsfundManager());
			userGroup.setGroupName(userGroupDto.getGroupName());

			List<UserGroupScreen> groupScreens = new ArrayList<UserGroupScreen>();
			if (screenids != null) {
				screenids = "{ \"screenids\" : ".concat(screenids) + " }";
				try {
					JSONObject jsonObject = new JSONObject(screenids);
					JSONArray jArray = jsonObject.getJSONArray("screenids");
					for (int i = 0; i < jArray.length(); i++) {
						JSONObject jObj = jArray.getJSONObject(i);
						if (jObj.getInt("menuid") != 0) {
							if (jObj.getInt("screenId") != 0) {

								UserGroupScreen groupScreen = new UserGroupScreen();
								UserGroupScreenPK groupScreenPK = new UserGroupScreenPK();

								groupScreenPK.setScreenId(jObj.getInt("screenId"));
								groupScreenPK.setUserGroupId(groupId);

								groupScreen.setId(groupScreenPK);
								groupScreen.setConfirm(jObj.getBoolean("confirm"));
								groupScreen.setReject(jObj.getBoolean("reject"));
								groupScreen.setSubmit(jObj.getBoolean("create"));
								groupScreen.setView(jObj.getBoolean("view"));

								groupScreens.add(groupScreen);
							}
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			Object object = userDaoImpl.updateUserGroup(userGroup, groupScreens);
			if (!(object instanceof Exception)) {
				userGroupDto.setSuccess(true);
				userGroupDto.setResultMsg("Update Successfully");
			} else {
				userGroupDto.setSuccess(false);
				userGroupDto.setResultMsg("There is some error returned from the server.");
			}
		} else {
			userGroupDto.setSuccess(false);
			userGroupDto.setResultMsg("Duplicate Code");
		}
		return userGroupDto.getResultMsg();
	}
}
